<?php
namespace Home\Controller;

class SuserController extends HomeController
{
  
    public function register(){
    	if(IS_POST){
            $user=D('User');
            $pic          = $this->upload();
            $_POST['file'] = $pic['file']['savepath'] . $pic['file']['savename'];
            //dump($_POST['file']);die;
            $res=$user->create(I('post.'));
            if($res){
                if($user->add()){
                  $this->success('添加成功',U('Suser/index'));
                }else{
                  $this->error('添加失败');	
                }
            }else{
            	$this->error($user->getError());
            }
    	}else{
    		$this->display();
    	}
    
    }
    public function ajuser(){
       $res=D('User')->field('user')->select();
       $arr=array();
       foreach ($res as $v) {
           $arr[]=$v['user'];
       }
       $user=I('post.user');
       //dump($res);
       //dump(in_array($user,$arr));die;
       $pattern='/([\x{4e00}-\x{9fa5}])|[a-zA-Z]/u';
       $html = $this->fetch('user');
       $html1 = $this->fetch('user1');
       if(preg_match($pattern,$user)&!in_array($user,$arr)){
            $return['status'] = 1;
            $return['info']   = 'success';
            $return['data']   = null;
       }elseif(!preg_match($pattern,$user)){
            $return['status'] = 0;
            $return['info']   = 'error';
            $return['data']   = $html;
       }elseif(in_array($user,$arr)){
            $return['status'] = 2;
            $return['info']   = 'error';
            $return['data']   = $html1;
       }
       $this->ajaxReturn($return);
    }
    public function ajemail(){
       $email=I('post.email');
       $res=D('User')->field('email')->select();
       $arr=array();
       foreach ($res as $v) {
           $arr[]=$v['email'];
       }
       $pattern='/\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/i';
       $html = $this->fetch('yz');
       $html1 = $this->fetch('email1');
       //dump(in_array($email,$arr));die;
       if(preg_match($pattern,$email)&!in_array($email,$arr)){
            $return['status'] = 1;
            $return['info']   = 'success';
            $return['data']   = null;
       }elseif(!preg_match($pattern,$email)){
            $return['status'] = 0;
            $return['info']   = 'error';
            $return['data']   = $html;
       }elseif(!in_array($eamil,$arr)){
            $return['status'] = 2;
            $return['info']   = 'error';
            $return['data']   = $html1;
       }
       $this->ajaxReturn($return);
    }
    public function ajtall(){
       $tall=I('post.tall');
       $res=D('User')->field('tall')->select();
       $arr=array();
       foreach ($res as $v) {
           $arr[]=$v['tall'];
       }
       $pattern='/0?(13|14|15|18)[0-9]{9}/';
       $html = $this->fetch('yz');
       $html = $this->fetch('tall1');
       if(preg_match($pattern,$tall)&!in_array($tall,$arr)){
            $return['status'] = 1;
            $return['info']   = 'success';
            $return['data']   = null;
       }elseif(!preg_match($pattern,$tall)){
            $return['status'] = 0;
            $return['info']   = 'error';
            $return['data']   = $html;
       }elseif(in_array($tall,$arr)){
            $return['status'] = 2;
            $return['info']   = 'error';
            $return['data']   = $html1;
       }
       $this->ajaxReturn($return);
    }
    public function ajaddress(){
       $address=I('post.address');
       $pattern='/[\x{4e00}-\x{9fa5}]/u';
       $html = $this->fetch('yz');
       if(preg_match($pattern,$address)){
            $return['status'] = 1;
            $return['info']   = 'success';
            $return['data']   = null;
       }else{
            $return['status'] = 0;
            $return['info']   = 'error';
            $return['data']   = $html;
       }
       $this->ajaxReturn($return);
    }
      public function ajpwd(){
       $pwd1=I('post.pwd1');
       $pwd2=I('post.pwd2');
       $html = $this->fetch('yzpwd');
       if($pwd1==$pwd2){
            $return['status'] = 1;
            $return['info']   = 'success';
            $return['data']   = null;
       }else{
            $return['status'] = 0;
            $return['info']   = 'error';
            $return['data']   = $html;
       }
       $this->ajaxReturn($return);
    }
    public function ajpwd1(){
       $pwd1=I('post.pwd1');
       $html = $this->fetch('yzpwd1');
       if($pwd1!=null){
            $return['status'] = 1;
            $return['info']   = 'success';
            $return['data']   = null;
       }else{
            $return['status'] = 0;
            $return['info']   = 'error';
            $return['data']   = $html;
       }
       $this->ajaxReturn($return);
    }
    public function upload()
    {
        $upload           = new \Think\Upload(); // 实例化上传类
        $upload->maxSize  = 3145728; // 设置附件上传大小
        $upload->exts     = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        // 上传文件
        $info = $upload->upload();
        if (!$info) {
            // 上传错误提示错误信息
            $this->error($upload->getError());
        } else {
            // 上传成功
            //$this->success('上传成功！');
            return $info;
        }
    }
    public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        if( in_array(C('USER_ADMINISTRATOR'), $id)){
            $this->error("不允许对超级管理员执行该操作!");
        }
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'forbiduser':
                $this->forbid('User', $map );
                break;
            case 'resumeuser':
                $this->resume('User', $map );
                break;
            case 'deleteuser':
                $this->delete('User', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }
    public function verify(){
    $verify = new \Think\Verify();
    $verify->entry(1);
  }
}